<template>
	<view class="d-login">
		
		<view class="d-login-btn">
			<template v-if="step == 1">
				<u-button class="d-btn" @click="back()" type="primary" shape="circle"  :custom-style="customStyle">确 定</u-button>
			</template>
		</view>
		<view class="d-login-btn1">
			<template v-if="step == 1">
				<u-button  type="primary" style="background-color:#4CD964">您的注册申请已经提交，后台会尽快审核您的信息。</u-button>
			</template>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
const obj = {
	0: {
		lable: '工人',
		value: 'any'
	},
	1: {
		label: '普通小中介',
		value: 'small'
	},
	2: {
		label: '专属小中介',
		value: 'small'
	},
	3: {
		label: '大中介',
		value: 'big'
	}
};
export default {
	data() {
		return {
			customStyle: {
				background: '#5650ee'
			},
			step: 1,
			type: 1, //普通小中介
			params: {
				sex: '',
				openId: '',
				sessionKey: '',
				encryptedData: '',
				iv: '',
				hidPic: '',
				realName: '',
				recId: '',
				rId: '',
				code: '',
				type: 0
			}
		};
	},
	onLoad(e) {
		console.log(e)
		if(e && e.type){
			this.params.type = e.type;
		}
		// this.form.rId = uni.getStorageSync('recId');
		this.recId = uni.getStorageSync('recId');
		var that = this;
		uni.login({
			provider: 'weixin',
			success: loginRes => {
				this.$u.api
					.postGetWechatOpenId({
						code: loginRes.code
					})
					.then(res => {
						console.log(res, '[登录页面-获取微信openid]');
						this.params.openId = res.data.openid;
						this.params.sessionKey = res.data.session_key;
						uni.setStorageSync('openId', this.params.openId);
						// if (!!res.data.phone) {
						// 	// 登录
						// 	this.postLoginCheck();
						// } else {
						// 	this.step = 1;
						// 	uni.showToast({
						// 		title: '请授权手机号',
						// 		icon: 'none'
						// 	})
						// }
					})
					.catch(err => {
						console.log(err);
					});
			}
		});
	},
	methods: {
		back(){
			uni.reLaunch({
				url:'index/index'
			})
		},
		// 判断用户是否登录
		postIsLogin() {
			this.$u.api
				.postIsLogin()
				.then(res => {
					if (res.code == 1000) {
						console.log(res, '[登录页面-判断用户是否登录]');
						// 获取用户信息
						this.postGetAdminInfo();
						uni.reLaunch({
							url: './index/home'
						});
					}
				})
				.catch(err => {
					console.log(err);
				});
		},
		// 获取微信用户信息
		getuserinfo(e) {
			this.params.sex = e.detail.userInfo.gender;
			this.params.hidPic = e.detail.userInfo.avatarUrl;
			this.params.realName = e.detail.userInfo.nickName;
			// 获取微信openid
			this.postGetWechatOpenId();
		},
		// 获取微信openid
		postGetWechatOpenId() {
			uni.login({
				provider: 'weixin',
				success: loginRes => {
					this.$u.api
						.postGetWechatOpenId({
							code: loginRes.code
						})
						.then(res => {
							console.log(res, '[登录页面-获取微信openid]');
							this.params.openId = res.data.openid;
							this.params.sessionKey = res.data.session_key;
							uni.setStorageSync('openId', this.params.openId);
							if (!!res.data.phone) {
								// 登录
								this.postLoginCheck();
							} else {
								this.step = 2;
								uni.showToast({
									title: '请授权手机号',
									icon: 'none'
								});
							}
						})
						.catch(err => {
							console.log(err);
						});
				}
			});
		},
		// 授权手机号
		getphonenumber(e) {
			console.log(e.phoneNumber);
			if (e.detail.errMsg == 'getPhoneNumber:ok') {
				this.params.encryptedData = e.detail.encryptedData;
				this.params.iv = e.detail.iv;
				this.params.recId = uni.getStorageSync('recId');
				this.params.rId = uni.getStorageSync('recId');
				// 登录
				this.postLoginCheck();
			} else {
				uni.alert('若拒绝无法体验更多功能');
			}
		},
		// 登录
		postLoginCheck() {
			let params = {
				...this.params
			};
			this.$u.api
				.postLoginCheck(params)
				.then(res => {
					if (res.status == true) {
						console.log(res, '[登录页面-登录]');
						uni.setStorageSync('token', res.data);
						// 获取用户信息
						this.postGetAdminInfo().then(res => {
							// uni.navigateBack()
							uni.reLaunch({
								url: './index/home'
							});
						});
					}
				})
				.catch(err => {
					console.log(err);
				});
		},
		// 获取用户信息
		postGetAdminInfo() {
			return new Promise((resolve, reject) => {
				this.$u.api
					.postGetAdminInfo()
					.then(res => {
						if (res.status == true) {
							uni.setStorageSync('userPhone', res.data.phone);
							// this.$store.commit('commitUserId', res.data.id);
							uni.setStorageSync('userId', res.data.id);
							uni.setStorageSync('userInfo', res.data);
							this.$store.commit('commitUserInfo', res.data);
							switch (res.data.type) {
								case 0:
									uni.setStorageSync('role', 'any');
									uni.setStorageSync('roleType', 0);
									break;
								case 1:
									uni.setStorageSync('role', 'small');
									uni.setStorageSync('roleType', 1);
									break;
								case 2:
									uni.setStorageSync('role', 'small');
									uni.setStorageSync('roleType', 2);
								case 3:
									uni.setStorageSync('role', 'big');
									uni.setStorageSync('roleType', 3);
							}
						}
						resolve();
					})
					.catch(err => {
						console.log(err);
						reject();
					});
			});
		}
	}
};
</script>

<style scoped lang="scss">
.d-login {
	position: fixed;
	top: 0;
	width: 100%;
	height: 100%;
	background: url()
		no-repeat left top;
	background-size: 100% auto;
	background-color: #fff;

	.d-login-btn {
		position: fixed;
		bottom: 170rpx;
		left: 0;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;

		.d-btn {
			width: 690rpx;
		}
	}
	.d-login-btn1 {
		position: relative;
 overflow: hidden;
    -webkit-line-clamp: 2;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
		top: 260rpx;
	
	width: 100%;
		.d-btn {
			width: 690rpx;
		}
	}
}
</style>
